package com.lovegu.two;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * <p>
 * 基于 JavaFx 实现计算器
 * </p>
 *
 * @author 老顾
 * @since 2023/3/29
 */
public class Calculator extends Application {

    private final TextField tfNumber1 = new TextField();
    private final TextField tfNumber2 = new TextField();
    private final TextField tfResult = new TextField();

    @Override
    public void start(Stage primaryStage) {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(10, 10, 10, 10));
        pane.setHgap(5);
        pane.setVgap(5);

        // 添加文本和数字按钮
        pane.add(new Label("数字1："), 0, 0);
        pane.add(tfNumber1, 1, 0);
        pane.add(new Label("数字2："), 0, 1);
        pane.add(tfNumber2, 1, 1);
        pane.add(new Label("结果："), 0, 2);
        pane.add(tfResult, 1, 2);
        tfResult.setEditable(false);

        // 添加菜单按钮
        Button btnAdd = new Button("加");
        Button btnSubtract = new Button("减");
        Button btnMultiply = new Button("乘");
        Button btnDivide = new Button("除");
        Button btnClear = new Button("清除");
        pane.add(btnAdd, 0, 3);
        pane.add(btnSubtract, 1, 3);
        pane.add(btnMultiply, 0, 4);
        pane.add(btnDivide, 1, 4);
        pane.add(btnClear, 1, 5);

        // 点击事件
        btnAdd.setOnAction(e -> add());
        btnSubtract.setOnAction(e -> subtract());
        btnMultiply.setOnAction(e -> multiply());
        btnDivide.setOnAction(e -> divide());
        btnClear.setOnAction(e -> clear());

        // 创建一个场景
        Scene scene = new Scene(pane);
        primaryStage.setTitle("计算器");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void add() {
        double num1 = Double.parseDouble(tfNumber1.getText());
        double num2 = Double.parseDouble(tfNumber2.getText());
        double result = num1 + num2;
        tfResult.setText(String.format("%.2f", result));
    }

    private void subtract() {
        double num1 = Double.parseDouble(tfNumber1.getText());
        double num2 = Double.parseDouble(tfNumber2.getText());
        double result = num1 - num2;
        tfResult.setText(String.format("%.2f", result));
    }

    private void multiply() {
        double num1 = Double.parseDouble(tfNumber1.getText());
        double num2 = Double.parseDouble(tfNumber2.getText());
        double result = num1 * num2;
        tfResult.setText(String.format("%.2f", result));
    }

    private void divide() {
        double num1 = Double.parseDouble(tfNumber1.getText());
        double num2 = Double.parseDouble(tfNumber2.getText());
        if (num2 == 0) {
            tfResult.setText("不能除于0");
        } else {
            double result = num1 / num2;
            tfResult.setText(String.format("%.2f", result));
        }
    }

    private void clear() {
        tfNumber1.setText("");
        tfNumber2.setText("");
        tfResult.setText("");
    }

    public static void main(String[] args) {
        launch(args);
    }
}
